package org.jeecg.modules.student.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.student.entity.DiyStudent;
import org.jeecg.modules.student.mapper.DiyStudentMapper;
import org.jeecg.modules.student.service.IDiyStudentService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 学生表
 * @Author: jeecg-boot
 * @Date:   2021-05-08
 * @Version: V1.0
 */
@Service
public class DiyStudentServiceImpl extends ServiceImpl<DiyStudentMapper, DiyStudent> implements IDiyStudentService {

    @Override
    public void saveOrUpdateInfo(JSONArray form_component_values, String originator_userid) {
        DiyStudent student = new DiyStudent();
        for (int i = 0; i < form_component_values.size(); i++) {
            JSONObject jsonObject = form_component_values.getJSONObject(i);
            String name = jsonObject.getString("name");
            String value = jsonObject.getString("value");

            switch (name){
                case "姓名":
                    student.setStuName(value);
                    break;
                case "学号":
                    student.setStuNumber(value);
                    break;
                case "系别":
                    student.setStuDepartment(value);
                    break;
                case "性别":
                    student.setStuSex(value);
                    break;
                case "专业":
                    student.setStuClass(value);
                    break;
                case "电话":
                    student.setStuPhone(value);
                    break;
            }
        }
        student.setStuDinguserid(originator_userid);
        System.out.println(student);
        List<DiyStudent> diyStudents = baseMapper.selectList(new LambdaQueryWrapper<DiyStudent>().eq(DiyStudent::getStuDinguserid, student.getStuDinguserid()));
        if (diyStudents!=null && diyStudents.size()>0){
            baseMapper.update(student,new LambdaQueryWrapper<DiyStudent>().eq(DiyStudent::getStuDinguserid,student.getStuDinguserid()));
        }else {
            baseMapper.insert(student);
        }
    }
}
